﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LiveNet.Contacts
{
    public class Location : DictionaryBase
    {
        #region Private Data
        private bool m_changed = false;
        #endregion

        #region Constructors
        internal Location()
        {
            BuildDictionary();
        }

        internal Location(XmlNode xmlnode)
        {
            BuildDictionary();
            FromXml(xmlnode);
        }

        private void BuildDictionary()
        {
            this.Add("ID", "");
            this.Add("LocationType", "");
            this.Add("Office", "");
            this.Add("Department", "");
            this.Add("CompanyName", "");
            this.Add("YomiCompanyName", "");
            this.Add("StreetLine", "");
            this.Add("StreetLine2", "");
            this.Add("PrimaryCity", "");
            this.Add("SecondaryCity", "");
            this.Add("SubDivision", "");
            this.Add("PostalCode", "");
            this.Add("CountryRegion", "");
            this.Add("Lattitude", "");
            this.Add("Longitude", "");
            this.Add("IsDefault", "");
        }
        #endregion

        #region Public Properties
        public int ID
        {
            get { return Convert.ToInt32(this["ID"]); }
            set { this["ID"] = Values.ToString(); }
        }

        public LocationType LocationType
        {
            get { return (LocationType)Enum.Parse(Type.GetType("LiveNet.Contacts.LocationType"), this["LocationType"]); }
            set { this["LocationType"] = value.ToString(); }
        }

        public string Office
        {
            get { return this["Office"]; }
            set { this["Office"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string Department
        {
            get { return this["Department"]; }
            set { this["Department"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string CompanyName
        {
            get { return this["CompanyName"]; }
            set { this["CompanyName"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string YomiCompanyName
        {
            get { return this["YomiCompanyName"]; }
            set { this["YomiCompanyName"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string StreetLine
        {
            get { return this["StreetLine"]; }
            set { this["StreetLine"] = value.Substring(0, Math.Min(256, value.Length)); }
        }

        public string StreetLine2
        {
            get { return this["StreetLine2"]; }
            set { this["StreetLine2"] = value.Substring(0, Math.Min(256, value.Length)); }
        }

        public string PrimaryCity
        {
            get { return this["PrimaryCity"]; }
            set { this["PrimaryCity"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string SecondaryCity
        {
            get { return this["SecondaryCity"]; }
            set { this["SecondaryCity"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string SubDivision
        {
            get { return this["SubDivision"]; }
            set { this["SubDivision"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string PostalCode
        {
            get { return this["PostalCode"]; }
            set { this["PostalCode"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public string CountryRegion
        {
            get { return this["CountryRegion"]; }
            set { this["CountryRegion"] = value.Substring(0, Math.Min(40, value.Length)); }
        }

        public decimal Lattitude
        {
            get { return Convert.ToDecimal(this["Lattitude"]); }
            set { this["Lattitude"] = value.ToString(); }
        }

        public decimal Longitude
        {
            get { return Convert.ToDecimal(this["Longitude"]); }
            set { this["Longitude"] = value.ToString(); }
        }

        public bool IsDefault
        {
            get { return Convert.ToBoolean(this["IsDefault"]); }
            set { this["IsDefault"] = value.ToString(); }
        }
        #endregion

        #region Public Methods
        public XmlDocument ToXml(OperationType op)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string xml = "<Location>";
            foreach (DictionaryEntry de in this)
            {
                if (op == OperationType.Create && de.Key.ToString() == "ID") continue;
                if (op == OperationType.Update && (de.Key.ToString() == "ID" || de.Key.ToString() == "LocationType")) continue;
                xml = xml + "<" + de.Key + ">" + de.Value + "</" + de.Key + ">";
            }
            xml = xml + "</Location>";
            xmlDoc.LoadXml(xml);
            return xmlDoc;
        }
        #endregion

        #region Internal Methods
        public void FromXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlNode node = xmlnode.SelectSingleNode("ID");
            if (node != null) this["ID"] = node.InnerText;
            node = xmlnode.SelectSingleNode("LocationType");
            if (node != null) this["LocationType"] = node.InnerText;
            node = xmlnode.SelectSingleNode("Office");
            if (node != null) this["Office"] = node.InnerText;
            node = xmlnode.SelectSingleNode("Department");
            if (node != null) this["Department"] = node.InnerText;
            node = xmlnode.SelectSingleNode("CompanyName");
            if (node != null) this["CompanyName"] = node.InnerText;
            node = xmlnode.SelectSingleNode("YomiCompanyName");
            if (node != null) this["YomiCompanyName"] = node.InnerText;
            node = xmlnode.SelectSingleNode("StreetLine");
            if (node != null) this["StreetLine"] = node.InnerText;
            node = xmlnode.SelectSingleNode("StreetLine2");
            if (node != null) this["StreetLine2"] = node.InnerText;
            node = xmlnode.SelectSingleNode("PrimaryCity");
            if (node != null) this["PrimaryCity"] = node.InnerText;
            node = xmlnode.SelectSingleNode("SecondaryCity");
            if (node != null) this["SecondaryCity"] = node.InnerText;
            node = xmlnode.SelectSingleNode("SubDivision");
            if (node != null) this["SubDivision"] = node.InnerText;
            node = xmlnode.SelectSingleNode("PostalCode");
            if (node != null) this["PostalCode"] = node.InnerText;
            node = xmlnode.SelectSingleNode("CountryRegion");
            if (node != null) this["CountryRegion"] = node.InnerText;
            node = xmlnode.SelectSingleNode("Lattitude");
            if (node != null) this["Lattitude"] = node.InnerText;
            node = xmlnode.SelectSingleNode("Longitude");
            if (node != null) this["Longitude"] = node.InnerText;
            node = xmlnode.SelectSingleNode("IsDefault");
            if (node != null) this["IsDefault"] = node.InnerText;
        }
        #endregion

        #region Dictionary Methods
        public String this[String key]
        {
            get { return ((String)Dictionary[key]); }
            set { Dictionary[key] = value; }
        }

        public ICollection Keys
        {
            get { return (Dictionary.Keys); }
        }

        public ICollection Values
        {
            get { return (Dictionary.Values); }
        }

        public void Add(String key, String value)
        {
            Dictionary.Add(key, value);
        }

        public bool Contains(String key)
        {
            return (Dictionary.Contains(key));
        }

        public void Remove(String key)
        {
            Dictionary.Remove(key);
        }

        protected override void OnInsert(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (value.GetType() != typeof(System.String))
                throw new ArgumentException("value must be of type String.", "value");
        }

        protected override void OnRemove(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");
        }

        protected override void OnSet(Object key, Object oldValue, Object newValue)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (newValue.GetType() != typeof(System.String))
                throw new ArgumentException("newValue must be of type String.", "newValue");
        }

        protected override void OnValidate(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (value.GetType() != typeof(System.String))
                throw new ArgumentException("value must be of type String.", "value");
        }
        #endregion
    }
}
